package main

import (
	"fmt"
	"time"
)

func apply01() {
	//创建channel
	intChan := make(chan int, 5)
	exitChan := make(chan bool, 1)

	go writeData(intChan)
	go readData(intChan, exitChan)

	for {
		value, ok := <-exitChan //ok == false 说明已经没值可以去的
		fmt.Printf("exitChan value is %v \n", value)
		fmt.Printf("----------------------------------- \n")
		if !ok {
			break
		}

	}
}

func writeData(intChan chan int) {
	for i := 1; i <= 50; i++ {
		intChan <- i //但channel 容量满了，会暂时阻塞
		fmt.Printf("writeData  write a data is %d \n", i)
		time.Sleep(time.Millisecond * 100)
	}
	close(intChan)
}

func readData(intChan chan int, exitChan chan bool) {
	for {
		value, ok := <-intChan
		if !ok {
			break
		}
		fmt.Printf("readData  read a data is %d \n", value)
		time.Sleep(time.Millisecond * 1000)
	}
	//读完数据，任务完成
	exitChan <- true
	close(exitChan)

}

func main() {

	apply01()

}
